import 'dart:io';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ASRCacheImage extends StatefulWidget {
  final String? image;
  final BoxFit fit;
  final bool cache;
  final String placeholder;
  final double? width;
  final double? height;
  final Alignment alignment;

  const ASRCacheImage({
    Key? key,
    this.image,
    this.alignment = Alignment.center,
    this.placeholder = 'assets/icon/placeholder.png',
    this.width,
    this.height,
    this.fit = BoxFit.fill,
    this.cache = true,
  }) : super(key: key);

  @override
  _ASRCacheImageState createState() {
    // TODO: implement createState
    return _ASRCacheImageState();
  }
}

class _ASRCacheImageState extends State<ASRCacheImage> {
  bool isExit = false;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.image != null) {
      File file = File(widget.image!);
      if (file.existsSync()) {
        isExit = true;
      }
    }
  }

  @override
  void didUpdateWidget(ASRCacheImage oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.image != widget.image) {
      if (widget.image != null) {
        File file = File(widget.image!);
        if (file.existsSync()) {
          isExit = true;
        }
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return widget.image == null || widget.image!.isEmpty
        ? Image.asset(
            widget.placeholder,
            fit: widget.fit,
            width: widget.width,
            height: widget.height,
          )
        : widget.image!.startsWith('http')
            ? ExtendedImage.network(
                widget.image!,
                fit: widget.fit,
                cache: widget.cache,
                width: widget.width,
                height: widget.height,
                alignment: widget.alignment,
                loadStateChanged: (ExtendedImageState state) {
                  switch (state.extendedImageLoadState) {
                    case LoadState.loading:
                      return Image.asset(
                        widget.placeholder,
                        fit: BoxFit.fill,
                      );
                      break;
                    case LoadState.completed:
                      return state.completedWidget;
                      break;
                    case LoadState.failed:
                      return GestureDetector(
                        child: Image.asset(
                          widget.placeholder,
                          fit: BoxFit.fill,
                        ),
                        onTap: () {
                          state.reLoadImage();
                        },
                      );
                      break;
                  }
                  return null;
                },
              )
            : widget.image!.startsWith('assets')
                ? Image.asset(
                    widget.image!,
                    fit: widget.fit,
                    width: widget.width,
                    height: widget.height,
                  )
                : isExit
                    ? ExtendedImage.file(
                        File(widget.image!),
                        fit: widget.fit,
                        width: widget.width,
                        height: widget.height,
                        alignment: widget.alignment,
                        loadStateChanged: (ExtendedImageState state) {
                          switch (state.extendedImageLoadState) {
                            case LoadState.loading:
                              return Image.asset(
                                widget.placeholder,
                                fit: BoxFit.fill,
                              );
                              break;
                            case LoadState.completed:
                              return state.completedWidget;
                              break;
                            case LoadState.failed:
                              return Image.asset(
                                widget.placeholder,
                                fit: BoxFit.fill,
                              );
                              break;
                          }
                          return null;
                        },
                      )
                    : Image.asset(
                        widget.placeholder,
                        fit: widget.fit,
                        width: widget.width,
                        height: widget.height,
                      );
  }
}
